package org.ligson.user.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SingleMessageListener {

    //1、定义fanout类型的交换器
    @Bean("defaultExchange")
    public DirectExchange defaultExchange() {
        return ExchangeBuilder.directExchange("defaultExchange").build();
        //return ExchangeBuilder.fanoutExchange("defaultExchange").build();
    }

    @Bean(name = "defaultQueue")
    public org.springframework.amqp.core.Queue defaultQueue() {
        return new org.springframework.amqp.core.Queue("defaultQueue");
    }

    //3、将两个不同名称的消息队列跟交换器进行绑定
    @Bean
    public Binding bindingSingle(@Qualifier("defaultQueue") org.springframework.amqp.core.Queue defaultQueue,
                                 @Qualifier("defaultExchange") DirectExchange defaultExchange) {
        return BindingBuilder.bind(defaultQueue).to(defaultExchange).with("defaultQueue");
    }

//    @RabbitListener(queuesToDeclare = {@Queue(declare = "true", name = "demo")}, ackMode = "AUTO")
//    public void onMessage(String msg) {
//        log.debug(msg);
//    }

    //    @RabbitListener(bindings = @QueueBinding(value = @org.springframework.amqp.rabbit.annotation.Queue("defaultQueue"),
//            exchange = @org.springframework.amqp.rabbit.annotation.Exchange(value = "defaultExchange", type = ExchangeTypes.DIRECT)), ackMode = "AUTO")
    @RabbitListener(queues = "defaultQueue", ackMode = "AUTO")
    public void onReceive(String msg) {
        log.debug(msg);
    }
}
